#ifndef AVL_H
#define AVL_H

#include <stdlib.h>

/* Key maximum size. */
#define MAX_KEY_SIZE 5

/* Null value for data_t */
#define DATA_NULL NULL

/* Payload for each key added */
typedef void * data_t;

/* Structure which stores the nodes of a tree. */
typedef struct _node_t
{
    char key[MAX_KEY_SIZE + 1];
    data_t data;

    struct _node_t *left;
    struct _node_t *right;

    int height;
    int removed;
} node_t;

/* Structure which stores the tree. */
typedef struct _avl_t
{
    size_t size;
    int key_limit;
    node_t *root;
} avl_t;

/* Creates an empty AVL tree. The maximum size of the key is 'key_limit'. */
avl_t *avl_create(int key_limit);

/* Destroys an AVL tree. */
void avl_destroy(avl_t *avl);

/**
 * Stores a word in the tree.
 * If the word already exists, it'll return the node (like in search).
 * Otherwise, it'll return the new node.
 */
node_t *avl_insert(avl_t *avl, const char *key, data_t data);

/* Returns the node associated to key. Returns NULL if it doesn't exists. */
node_t *avl_search(avl_t *avl, const char *key);

/* Remove the node associated to key. Does nothing if it doesn't exists. */
void avl_remove(avl_t *avl, const char *key);

#endif /* AVL_H */
